'use strict';
var y = Object.defineProperty;
var p = (o, r, e) =>
  r in o
    ? y(o, r, { enumerable: !0, configurable: !0, writable: !0, value: e })
    : (o[r] = e);
var h = (o, r, e) => (p(o, typeof r != 'symbol' ? r + '' : r, e), e);
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const v = require('../stringify/stringifyCollection.cjs'),
  M = require('./addPairToJSMap.cjs'),
  P = require('./Collection.cjs'),
  l = require('./identity.cjs'),
  m = require('./Pair.cjs'),
  g = require('./Scalar.cjs');
function d(o, r) {
  const e = l.isScalar(r) ? r.value : r;
  for (const t of o)
    if (
      l.isPair(t) &&
      (t.key === r || t.key === e || (l.isScalar(t.key) && t.key.value === e))
    )
      return t;
}
class S extends P.Collection {
  constructor(e) {
    super(l.MAP, e);
    h(this, 'items', []);
  }
  static get tagName() {
    return 'tag:yaml.org,2002:map';
  }
  static from(e, t, i) {
    const { keepUndefined: s, replacer: a } = i,
      f = new this(e),
      c = (n, u) => {
        if (typeof a == 'function') u = a.call(t, n, u);
        else if (Array.isArray(a) && !a.includes(n)) return;
        (u !== void 0 || s) && f.items.push(m.createPair(n, u, i));
      };
    if (t instanceof Map) for (const [n, u] of t) c(n, u);
    else if (t && typeof t == 'object')
      for (const n of Object.keys(t)) c(n, t[n]);
    return (
      typeof e.sortMapEntries == 'function' && f.items.sort(e.sortMapEntries), f
    );
  }
  add(e, t) {
    var f;
    let i;
    l.isPair(e)
      ? (i = e)
      : !e || typeof e != 'object' || !('key' in e)
        ? (i = new m.Pair(e, e == null ? void 0 : e.value))
        : (i = new m.Pair(e.key, e.value));
    const s = d(this.items, i.key),
      a = (f = this.schema) == null ? void 0 : f.sortMapEntries;
    if (s) {
      if (!t) throw new Error(`Key ${i.key} already set`);
      l.isScalar(s.value) && g.isScalarValue(i.value)
        ? (s.value.value = i.value)
        : (s.value = i.value);
    } else if (a) {
      const c = this.items.findIndex(n => a(i, n) < 0);
      c === -1 ? this.items.push(i) : this.items.splice(c, 0, i);
    } else this.items.push(i);
  }
  delete(e) {
    const t = d(this.items, e);
    return t ? this.items.splice(this.items.indexOf(t), 1).length > 0 : !1;
  }
  get(e, t) {
    const i = d(this.items, e),
      s = i == null ? void 0 : i.value;
    return (!t && l.isScalar(s) ? s.value : s) ?? void 0;
  }
  has(e) {
    return !!d(this.items, e);
  }
  set(e, t) {
    this.add(new m.Pair(e, t), !0);
  }
  toJSON(e, t, i) {
    const s = i ? new i() : t != null && t.mapAsMap ? new Map() : {};
    t != null && t.onCreate && t.onCreate(s);
    for (const a of this.items) M.addPairToJSMap(t, s, a);
    return s;
  }
  toString(e, t, i) {
    if (!e) return JSON.stringify(this);
    for (const s of this.items)
      if (!l.isPair(s))
        throw new Error(
          `Map items must all be pairs; found ${JSON.stringify(s)} instead`,
        );
    return (
      !e.allNullValues &&
        this.hasAllNullValues(!1) &&
        (e = Object.assign({}, e, { allNullValues: !0 })),
      v.stringifyCollection(this, e, {
        blockItemPrefix: '',
        flowChars: { start: '{', end: '}' },
        itemIndent: e.indent || '',
        onChompKeep: i,
        onComment: t,
      })
    );
  }
}
exports.YAMLMap = S;
exports.findPair = d;
